package com.example.tts.service;

import android.app.Service;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.LogUtils;
import com.example.tts.activity.MainActivity;

/**
 * 开机启动一个倒计时服务，倒计时50s之后跳转应用
 *
 * @author M1CH
 */

public class AutoStartService extends Service {
    CountDownTimer timer = new CountDownTimer(1 * 1000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            LogUtils.e("onTick");
        }

        @Override
        public void onFinish() {
            //结束的时候看应用是否在前台
            Intent splashIntent = new Intent(AutoStartService.this, MainActivity.class);
            splashIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(splashIntent);
            onDestroy();
        }
    };


    @Override
    public void onCreate() {
        super.onCreate();
        timer.start();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy() {
        timer.cancel();
        super.onDestroy();
    }
}
